package top.xana.acg.music.ui.page

import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator
import top.xana.acg.common.base.adp.BaseDelegateAdapter
import top.xana.acg.common.base.adp.BaseViewHolder
import top.xana.acg.common.base.adp.RecyclerAdapter
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.common.utils.*
import top.xana.acg.music.R
import top.xana.acg.music.data.*
import top.xana.acg.music.databinding.FindFragmentBinding


@SuppressWarnings("all")
class FindFragment : BaseFragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    private val HOME_VIEW_TYPE_MENU = 1

    override fun initView() {
        super.initView()
        val binding = getBinding<FindFragmentBinding>()
        RecyclerView.RecycledViewPool().let {
            it.setMaxRecycledViews(1, 6)
            it.setMaxRecycledViews(2, 6)
            it.setMaxRecycledViews(3, 6)
            it.setMaxRecycledViews(4, 6)
            it.setMaxRecycledViews(5, 6)
            it.setMaxRecycledViews(6, 6)
            it.setMaxRecycledViews(7, 6)
            it.setMaxRecycledViews(8, 6)
            it.setMaxRecycledViews(9, 6)
            binding.recyclerView.setRecycledViewPool(it)
        }
// 2
        val layoutManager = VirtualLayoutManager(requireActivity()).also {
            binding.recyclerView.layoutManager = it
        }
        val adapters = DelegateAdapter(layoutManager)
        binding.recyclerView.adapter = adapters


        val draws = arrayOf(
            R.drawable.t_dragonball_icn_daily,
            R.drawable.t_dragonball_icn_fm,
            R.drawable.t_dragonball_icn_playlist,
            R.drawable.t_dragonball_icn_rank,
            R.drawable.t_dragonball_icn_look,
            R.drawable.t_dragonball_icn_digitalalbum,
            R.drawable.t_dragonball_icn_digitalalbum,
            R.drawable.t_dragonball_icn_radio,
            R.drawable.t_dragonball_icn_radio
        )

        val pList = List(18) { PlayList("", "", "") }
        val mList = List(9) { Music("", "", "", "") }
        adapters.addAdapterSeq(
            HomeBannerAdapter(
                listOf(
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner(""),
                    HomeBanner("")
                ), this
            ),
            MenuAdapter(
                resources.getStringArray(R.array.findMenuArray)
                    .mapIndexed { i, s -> Menu(draws[i], s, R.id.to_daily_recommend) }),

            LabAdapter(Label("推荐歌单", "查看更多")),
            RecomendPlayListAdapter(pList),
            LabAdapter(Label("感受二次元世界", "播放全部")),
            EliteMusicListAdapter(mList),
            LabAdapter(Label("某傲娇的miko的雷达歌单", "查看更多")),
            RecomendPlayListAdapter(pList),
            LabAdapter(Label("LOOK直播", "查看更多")),
            RecomendPlayListAdapter(pList),
            LabAdapter(Label("新歌", "更多新歌")),
            EliteMusicListAdapter(mList),

            LabAdapter(Label("排行榜", "查看更多")),
            EliteMusicListAdapter(mList),
            LabAdapter(Label("24小时播客", "查看更多")),
            BokeAdapter(
                listOf(
                    Boke("", ""),
                    Boke("", ""),
                    Boke("", ""),
                    Boke("", ""),
                    Boke("", ""),
                    Boke("", ""),
                    Boke("", "")
                )
            )
        )
    }

    class LabAdapter(val lab: Label) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_label_bar, 1) {
        override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
            super.onBindViewHolder(holder, position)
            holder.getView<TextView>(R.id.tv_label).text = lab.labName
            holder.getView<TextView>(R.id.btn_label).apply {
                text = lab.more
                if (text == "播放全部")
                    setDrawableStart(R.drawable.ic_find_lab_play_all)
            }
        }
    }

    /**
     * 推荐歌单
     */
    class RecomendPlayListAdapter(private val palyLists: List<PlayList>) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_recyclerview, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<RecyclerView>(R.id.recyclerview_hor).apply {
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                val mg = layoutParams as ViewGroup.MarginLayoutParams
                mg.topMargin = dp2px(R.dimen.dp_10)
                mg.bottomMargin = dp2px(R.dimen.dp_10)
                addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
                        val i = parent.getChildAdapterPosition(view)
                        if (i == 0)
                            outRect.left = dp2px(R.dimen.dp_15)
                        else if (i == palyLists.size - 1) {
                            outRect.right = dp2px(R.dimen.dp_15)
                            outRect.left = dp2px(R.dimen.dp_14)
                        } else outRect.left = dp2px(R.dimen.dp_10)
                    }
                })
                adapter = object : RecyclerAdapter<PlayList>() {
                    override fun getItemViewType(position: Int, data: PlayList) =
                        R.layout.layout_item_find_play_list

                    override fun onCreateViewHolder(
                        view: View,
                        viewType: Int
                    ) = object : RecyclerAdapter.ViewHolder<PlayList>(view) {
                        override fun onBind(data: PlayList) {
                            // todo data from repo
                            getView<ImageView>(R.id.riv_cover).setImageDrawable(
                                resources.getDrawable(
                                    R.drawable.noa
                                )
                            )
                            getView<TextView>(R.id.tv_title).setText(R.string.label_tips)
                            getView<TextView>(R.id.tv_count).setText(R.string.label_count)
                        }
                    }
                }.also { it.addAll(palyLists) }
            }
            return holder
        }
    }

    /**
     * 菜单
     */
    class MenuAdapter(
        private val menus: List<Menu>
    ) : BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_recyclerview, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val viewHolder = super.onCreateViewHolder(parent, viewType)
            viewHolder.getView<RecyclerView>(R.id.recyclerview_hor).apply {
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                adapter = object : RecyclerAdapter<Menu>(object :OnItemListenerImpl<Menu>(){
                    override fun onItemClick(holder: ViewHolder<Menu>, data: Menu) {
                        holder.itemView.navTo(data.destId)
                    }
                }) {

                    override fun getItemViewType(position: Int, data: Menu) =
                        R.layout.layout_item_find_menu

                    override fun onCreateViewHolder(view: View, viewType: Int) =
                        object : RecyclerAdapter.ViewHolder<Menu>(view) {
                            override fun onBind(data: Menu) {
                                getView<TextView>(R.id.tv_find_menu).text = data.menuName
                                getView<ImageView>(R.id.iv_find_menu).setImageDrawable(
                                    resources.getDrawable(
                                        data.draw
                                    )
                                )
                            }
                        }
                }.apply { addAll(menus) }
            }
            return viewHolder
        }
    }

    class EliteMusicListAdapter(val musics: List<Music>) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_recyclerview, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<RecyclerView>(R.id.recyclerview_hor).apply {
                val mg = layoutParams as ViewGroup.MarginLayoutParams
                mg.topMargin = dp2px(R.dimen.dp_5)
                mg.bottomMargin = dp2px(R.dimen.dp_18)
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                adapter = object : RecyclerAdapter<String>() {
                    override fun getItemViewType(position: Int, data: String) =
                        R.layout.vlayout_recyclerview_ver

                    private var r = 0;
                    override fun onCreateViewHolder(view: View, viewType: Int) =
                        object : RecyclerAdapter.ViewHolder<String>(view) {
                            override fun onBind(data: String) {
                                getView<RecyclerView>(R.id.recyclerview_ver).apply {
                                    layoutManager = LinearLayoutManager(context)
                                    adapter = object : RecyclerAdapter<Music>() {
                                        override fun getItemViewType(position: Int, data: Music) =
                                            R.layout.layout_item_find_elite_music

                                        override fun onCreateViewHolder(
                                            view: View,
                                            viewType: Int
                                        ) = object : RecyclerAdapter.ViewHolder<Music>(view) {
                                            override fun onBind(data: Music) {
                                                getView<ImageView>(R.id.riv_cover).setImageDrawable(
                                                    resources.getDrawable(R.drawable.noa)
                                                )
                                                getView<TextView>(R.id.tv_title).setText(R.string.label_tips)
                                                getView<TextView>(R.id.tv_creator).setText(R.string.label_noa)
                                                getView<TextView>(R.id.tv_desc).setText(R.string.label_tips)
                                                getView<ImageView>(R.id.iv_play_btn).setImageDrawable(
                                                    resources.getDrawable(R.drawable.sel_elite_play)
                                                )
                                            }
                                        }
                                    }.also {
                                        for (i in 0..2) {
                                            r += i
                                            it.add(musics[i])
                                        }
                                    }
                                }
                            }
                        }
                }.also { it.add("", "", "") }
            }
            return holder
        }
    }

    class BokeAdapter(val bokes: List<Boke>) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_recyclerview, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<RecyclerView>(R.id.recyclerview_hor).apply {
                layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                adapter = object : RecyclerAdapter<Boke>() {
                    override fun getItemViewType(position: Int, data: Boke) =
                        R.layout.layout_item_find_boke

                    override fun onCreateViewHolder(view: View, viewType: Int) =
                        object : RecyclerAdapter.ViewHolder<Boke>(view) {
                            override fun onBind(data: Boke) {
                                getView<ImageView>(R.id.riv_cover).let {
                                    it.setImageDrawable(resources.getDrawable(R.drawable.noa))
                                    it.radius(R.dimen.dp_50)
                                }
                                getView<TextView>(R.id.tv_title).setText(R.string.label_tips)
                            }
                        }
                }.also { it.addAll(bokes) }
            }
            return holder
        }
    }


    class BannerAdp(banners: List<HomeBanner>) :
        BannerAdapter<HomeBanner, BaseViewHolder>(banners) {
        override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder {
            val view = LayoutInflater.from(parent!!.context).inflate(
                R.layout.layout_item_home_banner, parent,
                false
            )
            view.radius(R.dimen.dp_8)
            return BaseViewHolder(view)
        }

        override fun onBindView(
            holder: BaseViewHolder,
            data: HomeBanner?,
            position: Int,
            size: Int
        ) {
            holder.getView<ImageView>(R.id.iv_banner).setDrawable(R.drawable.yukinoa)
        }
    }

    class HomeBannerAdapter(val banners: List<HomeBanner>, val lifecycleOwner: LifecycleOwner) :
        BaseDelegateAdapter(LinearLayoutHelper(), R.layout.vlayout_banner, 1) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            holder.getView<Banner<HomeBanner, BannerAdp>>(R.id.find_banner).apply {
                radius(R.dimen.dp_8)
                adapter = BannerAdp(banners)
                val mg = layoutParams as ViewGroup.MarginLayoutParams
                mg.topMargin = dp2px(R.dimen.dp_84)
                addBannerLifecycleObserver(lifecycleOwner)
                indicator = CircleIndicator(context)
                addItemDecoration(object: RecyclerView.ItemDecoration(){
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
//                        if(parent.getChildAdapterPosition(view)< banners.size-1)
//                            outRect.right = dp2px(R.dimen.dp_10)
                    }
                })
            }
            return holder
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() = FindFragment()

        const val TAG = "FindFragment"
    }

    override fun getLayoutId() = R.layout.find_fragment
}